var prefix = "/centre/manage";
$(function () {
    load();
});
$('#centresTable').on('load-success.bs.table', function (e, data) {
    if (data.total && !data.rows.length) {
        $('#centresTable').bootstrapTable('selectPage').bootstrapTable('refresh');
    }
});

function load() {
    $('#centresTable').bootstrapTable(
        {
            method: 'get', // 服务器数据的请求方式 get or post
            url: prefix + "/list", // 服务器数据的加载地址
            showRefresh: true,
            // showToggle : true,
            iconSize: 'outline',
            // toolbar: '#exampleToolbar',
            striped: true, // 设置为true会有隔行变色效果
            dataType: "json", // 服务器返回的数据类型
            pagination: true, // 设置为true会在底部显示分页条
            pageList: [10, 20, 50],
            // queryParamsType : "limit",
            // //设置为limit则会发送符合RESTFull格式的参数
            singleSelect: false, // 设置为true将禁止多选
            // contentType : "application/x-www-form-urlencoded",
            // //发送到服务器的数据编码类型
            pageSize: 10, // 如果设置了分页，每页数据条数
            pageNumber: 1, // 如果设置了分布，首页页码
            // search : true, // 是否显示搜索框
            showColumns: true, // 是否显示内容下拉框（选择显示的列）
            sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者
            // "server"
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: params.offset,
                    order: 'desc',
                    name: $.trim($("#name").val()),
                    //status: $.trim($('#status').val()),
                    type: $('#type').val()
                };
            },
            // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
            // queryParamsType = 'limit' ,返回参数必须包含
            // limit, offset, search, sort, order 否则, 需要包含:
            // pageSize, pageNumber, searchText, sortName,
            // sortOrder.
            // 返回false将会终止请求
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'centreId', // 列字段名
                    title: '商户编号' // 列标题
                },
                {
                    field: 'name', // 列字段名
                    title: '商户名称' // 列标题
                },
                {
                    field: 'shortName',
                    title: '简称'
                },
                {
                    field: 'projectexpName',
                    title: '版本',
                    formatter: function (value, row, index) {
                        if (value == "" || value == null) {
                            return '后台添加'
                        } else {
                            return value
                        }
                    }
                },
                {
                    field: 'depositBank',
                    title: '开户行'
                },
                {
                    field: 'bankNo',
                    title: '银行账号'
                },
                {
                    field: 'legalName',
                    title: '法人姓名'
                },
                {
                    field: 'accountMobile',
                    title: '联系电话'
                },
                {
                    field: 'legalNo',
                    title: '法人身份证'
                },
                {
                    field: 'registAddress',
                    title: '注册地址'
                },
                {
                    field: 'payStatus',
                    title: '支付状态',
                    formatter: function (value, row, index) {
                        if (row.mid == "" || row.mid == null) {
                            return '<font color="red">后台添加</font>'
                        } else {
                            if (value == '1') {
                                return '<font color="red">支付中</font>'
                            } else if (value == '0') {
                                return '<font color="red">未支付</font>'
                            } else if (value == '2') {
                                return '<font color="red">已完成</font>'
                            } else if (value == '3') {
                                return '<font color="red">支付失败</font>'
                            }
                        }
                    }
                },
                {
                    field: 'type',
                    title: '类型',
                    formatter: function (value, row, index) {
                        if (value) {
                            if (value == '1') {
                                return '企业'
                            } else if (value == '0') {
                                return '个人'
                            } else {

                            }
                        }
                    }
                },
                {
                    field: 'hide',
                    title: '显示',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return '隐藏'
                        } else if (value == 0) {
                            return '显示'
                        } else {

                        }
                    }
                },
                // {
                //     field: 'paymentdays',
                //     title: '打款期限',
                //     formatter: function (value, row, index) {
                //         return value+"天"
                //     }
                // },
                {
                    field: 'salesmanName',
                    title: '所属业务员'
                },
                {
                    field: 'branchOfficeName',
                    title: '所属分公司'
                },
                {
                    field: 'createTime',
                    title: '登记时间'
                }, {
                    field: 'amount',
                    title: '账户余额',
                    formatter: function (value, row, index) {

                        return '<a href="#" onclick="balancelog(' + row.centreId + ')">' + value + '</a>';
                    }
                }, {
                    field: 'verify',
                    title: '商品审核',
                    formatter: function (value, row, index) {
                        if (value === 1) {
                            return "审核";
                        } else {
                            return "不审核"
                        }
                    }
                },
                {
                    title: '操作',
                    field: 'centreId',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var r = "";
                        var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                            + value + '\')"><i class="fa fa-edit"></i>编辑</a> ';
                        var bs = "";
                        if (!row.salesmanId) {
                            var bs = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="绑定业务员" onclick="bindSalesman(\''
                                + value + '\')"><i class="fa fa-edit"></i>绑定业务员</a> ';
                        }
                        var dy = '<a class="btn btn-success btn-sm ' + s_detail_h + '" href="#" mce_href="#" title="详情" onclick="detail(\''
                            + value + '\')">' + '<i class="fa fa fa-eye"></i>详情</a> ';

                        if (row.status == 1) {
                            var d = '<a class="btn btn-warning btn-sm ' + s_remove_h + '" href="#" title="停用"  mce_href="#" onclick="remove(\''
                                + value + '\')"><i class="fa fa-remove"></i>停用</a>';
                        }
                        if (row.status == 2) {
                            var d = '<a class="btn btn-success btn-sm ' + s_check_h + '" href="#" title="审核"  mce_href="#" onclick="check(\''
                                + value + '\',\''
                                + row.mid + '\',\''
                                + row.payStatus + '\')"><i class="fa fa-check"></i>审核</a>';

                            r = '<a class="btn btn-warning btn-sm ' + s_remove_h + '" href="#" title="拒绝"  mce_href="#" onclick="refuse(\''
                                + value + '\')"><i class="fa fa-remove"></i>拒绝</a>';
                        }

                        if (row.status == 0) {
                            var d = '<a class="btn btn-warning btn-sm ' + s_resume_h + '" href="#" title="恢复"  mce_href="#" onclick="resume(\''
                                + value + '\')"><i class="fa fa-remove"></i>恢复</a>';
                        }
                        if (row.status == 1) {
                            var cz = '<a class="btn btn-success btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="充值" onclick="recharge(\''
                                + value + '\')">' + '<i class="fa fa fa-eye"></i>充值</a> ';
                        }
                        if (row.status == 2) {
                            return e + dy + d + r;
                        } else if (row.status == 3) {
                            return e;
                        } else if (row.status == 0) {
                            return e + d;
                        } else if (row.status == 1) {
                            return e + dy + d + cz + r + bs;
                        } else {
                            return e + dy + bs;
                        }
                    }
                }]
        });
}

function reLoad() {
    $('#centresTable').bootstrapTable('refresh');
}

/**
 * 恢复
 * @param centreId
 */
function resume(centreId) {
    layer.confirm('确定要恢复选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/resume",
            type: "post",
            data: {
                'centreId': centreId
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function remove(centreId) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'centreId': centreId
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}


function refuse(centreId) {
    layer.confirm('确定要拒绝选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/update",
            type: "post",
            data: {
                'centreId': centreId,
                "status": 3
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function check(centreId, mid, payStatus) {
    if (payStatus != 2) {
        alert("未支付");
        return;
    }
    //alert(mid);
    layer.confirm('确定要审核选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        //防止重复点击
        layer.open({type: 1, title: false, closeBtn: false});
        $.ajax({
            url: prefix + "/check",
            type: "post",
            data: {
                'centreId': centreId
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                    //关闭所有弹窗
                    layer.closeAll();
                } else {
                    layer.msg(r.msg);
                    //关闭所有弹窗
                    layer.closeAll();
                }
            }
        });
    })
}

function batchRemove() {
    var rows = $('#centresTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0) {
        layer.msg("请选择要删除的数据");
        return;
    }
    layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        var ids = [];
        // 遍历所有选择的行数据，取每条数据对应的ID
        $.each(rows, function (i, row) {
            ids[i] = row['centreId'];
        });
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids
            },
            url: prefix + '/batchRemove',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }, function () {
    });
}

function add() {
    // iframe层
    layer.open({
        id: 'centre_add',
        type: 2,
        title: '添加商户',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['80%', '90%'],
        content: prefix + '/add' // iframe的url
    });
}

function edit(centreId) {
    layer.open({
        id: 'centre_edit',
        type: 2,
        title: '修改商户',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: prefix + '/edit/' + centreId // iframe的url
    });
}

function detail(centreId) {
    layer.open({
        id: 'centre_detail',
        type: 2,
        title: '商户详情',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: prefix + '/detail/' + centreId // iframe的url
    });
}

function balancelog(centreId) {
    /*if(mid==null||mid==""){
        alert("该供应商未绑定会员!")
        return;
    }*/
    layer.open({
        id: 'balancelog',
        type: 2,
        title: '商户记录',
        maxmin: true,
        shadeClose: false,
        area: ['90%', '80%'],
        content: '/centre/amountLog/toLogs/' + centreId // iframe的url
    });
}

function batchHide(hide) {
    var rows = $('#centresTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    /*	if (rows.length == 0) {
            layer.msg("请选择要隐藏的数据");
            return;
        }*/
    var str = "";
    if (hide == 1) {
        str = "确认要隐藏选中的" + rows.length + "'条数据吗?";
    } else {
        str = "确认要显示选中的" + rows.length + "'条数据吗?";
    }
    if (rows.length == 0) {
        if (hide == 1) {
            str = "确认要隐藏全部";
        } else {
            str = "确认要显示选全部";
        }
    }
    layer.confirm(str, {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        var ids = new Array();
        if (rows.length != 0) {
            // 遍历所有选择的行数据，取每条数据对应的ID
            $.each(rows, function (i, row) {
                ids[i] = row['centreId'];
            });
        } else {
            ids[0] = 0;
        }
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids,
                "hide": hide
            },
            url: prefix + '/batchHiden',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }, function () {
    });
}

function recharge(centreId) {
    layer.open({
        id: 'centre_recharge',
        type: 2,
        title: '商户充值',
        maxmin: true,
        shadeClose: false,
        area: ['40%', '55%'],
        content: prefix + '/recharge/' + centreId
    });
}

function bindSalesman(centreId) {
    layer.open({
        id: 'bind_salesman',
        type: 2,
        title: '绑定业务员',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: '/salesman/manage/toBindSalesman/' + centreId
    });
}

function loadSalesmanInfo(salesmanId, centreId) {
    $.ajax({
        url: prefix + "/bindSalesman",
        type: "post",
        data: {
            'centreId': centreId,
            'salesmanId': salesmanId,
        },
        beforeSend: function (request) {
            index = layer.load();
        },
        success: function (r) {
            if (r.code == 0) {
                layer.close(index);
                layer.msg(r.msg);
                reLoad();
            } else {
                layer.msg(r.msg);
            }
        }
    });
}